package br.com.afi.encoding_converter.console.interpreter;

import java.text.MessageFormat;
import java.util.Map;

import br.com.afi.encoding_converter.Command;
import br.com.afi.pattern.interpreter.expression.AbstractProgressiveResultExpression;
import br.com.afi.pattern.interpreter.expression.Expression;

/**
 * Expressão que representa um comando que recebe uma outra expressão.
 * 
 * @author André de Fontana Ignacio
 * @version 1.0
 */
public class ComplexCommandExpression extends AbstractProgressiveResultExpression{
	
	private static final String FILE_FILTER = "f";
	private static final String FROM_ENCODING = "fe";
	private static final String TO_ENCODING = "te";
	private static final String FROM_DIRECTORY = "fd";
	private static final String TO_DIRECTORY = "td";
	public static final String REGEX = MessageFormat.format( "-({0}|{1}|{2}|{3}|{4})", FILE_FILTER,FROM_ENCODING, TO_ENCODING, FROM_DIRECTORY, TO_DIRECTORY );
	
	private String str;
	private Expression expression;
	
	/**
	 * Construtor
	 * 
	 * @param str String
	 * @param expression Expressão
	 * @param command Objeto que recebe os dados interpretados
	 */
	public ComplexCommandExpression( String str, Expression expression, Command command )
	{
		super();
		this.str = str;
		this.expression = expression;
		setResult(command);
	}
	
	@Override
	public Object interpret( Map<String, Object> variables ) 
	{
		final Command command = getResult( Command.class );
		if( FILE_FILTER.equals( str ) )
		{
			command.setFileFilterPattern( (String) expression.interpret( variables ) );
		}
		else if( FROM_ENCODING.equals( str ) )
		{
			command.setFromEncoding( (String) expression.interpret( variables ) );
		}
		else if( TO_ENCODING.equals( str ) )
		{
			command.setToEncoding( (String) expression.interpret( variables ) );
		}
		else if( TO_DIRECTORY.equals( str ) )
		{
			command.setToDirectory( (String) expression.interpret( variables ) );
		}
		else if( FROM_DIRECTORY.equals( str ) )
		{
			command.setFromDirectory( (String) expression.interpret( variables ) );
		}
		
		return command;
	}

	@Override
	public String toString() {
		return MessageFormat.format("expression={0}, str={1}", expression, str);
	}
}
